﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;


namespace frmCheckStock
{
    public partial class frmDisbursementList : Form
    {
        public frmDisbursementList()
        {
            InitializeComponent();
        }



        private void frmDisbursementList_Load(object sender, EventArgs e)
        {
            LogicUniversityT2Entities context = new LogicUniversityT2Entities();
            var x = context.Departments;
            cboDepartment.DataSource = x;
            cboDepartment.DisplayMember = "DepartmentName";

        }

        private void cboDepartment_SelectedIndexChanged(object sender, EventArgs e)
        {
            LogicUniversityT2Entities context = new LogicUniversityT2Entities();

            Department d = (Department)cboDepartment.SelectedItem;

            var itemList = from departmentRequi in context.DepartmentRequisitions
                           join item in context.Items
                                 on departmentRequi.ItemID equals
                                 item.ItemID
                           where departmentRequi.DepartmentID == d.DepartmentID
                           select new
                           {
                               ItemID = departmentRequi.ItemID,
                               ItemDescription = departmentRequi.Item.ItemDescription,
                               RequestedQuantity = departmentRequi.RequestedQuantity,
                            // DeliveredQuantity = departmentRequi.DeliveredQuantity,
                            ApprovedDate = departmentRequi.ApproveDate,  
                            StockBalance = departmentRequi .Item.StockBalance ,
                           };

             var itemlist = itemList.ToList();
            gvDisbursementList.DataSource = itemlist;

            var itemList1 = from depart in context.DepartmentRequisitions
                            join item in context.Items
                            on depart.ItemID equals item.ItemID
                            where depart.DepartmentID == d.DepartmentID
                            select new
                            {
                                ItemID = depart.ItemID,
                                ItemDescription = depart.Item.ItemDescription,
                                CollectedQuantity = depart.CollectedQuantity,
                                StockBalance = depart.Item.StockBalance,
                            };


            var itemlist1 = itemList1.ToList();
            gvOutstandStock.DataSource = itemlist1;
            
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {

            LogicUniversityT2Entities context = new LogicUniversityT2Entities();
            DepartmentRequisition dr = new DepartmentRequisition();
            Item item = new Item();
            int i1 = 0;
            for (int i = 0; i < gvDisbursementList.Rows.Count; i++)
            {
                DateTime dt = Convert.ToDateTime(gvDisbursementList.Rows[i].Cells[4].Value);
               

                    int b = Convert.ToInt16(gvDisbursementList.Rows[i].Cells[0].Value.ToString());
                   
                       
                    i1+= Convert.ToInt16(gvDisbursementList.Rows[i].Cells[0].Value);
                 
           
   
                    
                    DepartmentRequisition department = (from d in context.DepartmentRequisitions
                                                        where d.ApproveDate == dt
                                                        select d).First<DepartmentRequisition>();

                    department.DeliveredQuantity = b;
                    context.SaveChanges();

                    //int s = Convert.ToInt32(gvOutstandStock.Rows[i].Cells[3].Value);
                    string itemid = (String)gvOutstandStock.Rows[i].Cells[0].Value;

                    Item it = (from ie in context.Items
                               where ie.ItemID == itemid
                               select ie).First<Item>();

                    it.StockBalance = Convert.ToInt32(gvDisbursementList.Rows[i].Cells[5].Value) - i1 ;
                    context.SaveChanges();


                 Department dd = (Department)cboDepartment.SelectedItem;



                var itemList1 = from depart in context.DepartmentRequisitions
                                join itm in context.Items
                                on depart.ItemID equals itm.ItemID
                                where depart.DepartmentID == dd.DepartmentID
                                select new
                                {
                                    ItemID = depart.ItemID,
                                    ItemDescription = depart.Item.ItemDescription,
                                    CollectedQuantity = depart.CollectedQuantity,
                                    StockBalance = depart.Item.StockBalance,
                                };


                var itemlist1 = itemList1.ToList();
                gvOutstandStock.DataSource = itemlist1;


            }

            
        }

      }
    }

 